package be.xios.projectquiz.jury;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.table.AbstractTableModel;


/// <summary>
/// Deze klasse geeft info over de database.
/// </summary>
/// <author>Flemming Bosch</author>
public class ResultSetTableModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	private Connection connection;
	private Statement statement;
	private ResultSet resultSet;
	private ResultSetMetaData metaData;
	private int numberOfRows;
	
	private boolean connectedToDatabase = false;
	
	/// <summary>
	/// Deze method maakt een connectie met de database.
	/// </summary>
	/// <parameter name="url">het ip van de DB</parameter>
	/// <parameter name="username">de poort van de DB</parameter>
	/// <parameter name="password">het wachtwoord van de DB</parameter>
	/// <author>Flemming Bosch</author>
	public ResultSetTableModel(String url,String username,String password,String query) throws SQLException {
		
		connection = DriverManager.getConnection(url, username, password);
		statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		
		connectedToDatabase=true;
		setQuery(query);
	}

	/// <summary>
	/// Deze klasse krijgt info over de kolom van de DB
	/// </summary>
	/// <parameter name="column">kolomnummer</parameter>
	/// <author>Flemming Bosch</author>
	public Class<?> getColumnClass(int column) throws IllegalStateException
	{
		if(!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");
		
		try{
			String className= metaData.getColumnClassName(column+1);
			return Class.forName(className);
		}
		catch( Exception ex){
			ex.printStackTrace();
		}
		return Object.class;
	}
	
	/// <summary>
	/// Deze method verkrijgt het aantal kolommen van de database
	/// </summary>
	/// <return>kolomnummer</return>
	/// <author>Flemming Bosch</author>
	@Override
	public int getColumnCount() throws IllegalStateException
	{
		if(!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");
		
		try{
			return metaData.getColumnCount();
		}
		catch(SQLException ex){
			ex.printStackTrace();
		}
		
		return 0;
	}

	/// <summary>
	/// Deze method verkrijgt de kolomnaam
	/// </summary>
	///	<parameter name="column">kolomnummer</parameter>
	/// <return>kolomnaam</return>
	/// <author>Flemming Bosch</author>
	public String getColumnName(int column) throws IllegalStateException
	{
		if(!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");
		
		try{
			return metaData.getColumnName(column+1);
		}
		catch(SQLException ex){
			ex.printStackTrace();
		}
		return "";
	}
	
	/// <summary>
	/// Deze method verkrijgt het aantal rijen
	/// </summary>
	/// <return>integer van aantal rijen</return>
	/// <author>Flemming Bosch</author>
	@Override
	public int getRowCount() throws IllegalStateException
	{
		if(!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");

		return numberOfRows;
	}

	/// <summary>
	/// Deze method verkrijgt de data van de opgegeven rij en kolom.
	/// </summary>
	///	<parameter name="row">rijnummer</parameter>
	///	<parameter name="column">kolomnummer</parameter>
	/// <return>Object van de opgegeven rij en kolom</return>
	/// <author>Flemming Bosch</author>
	@Override
	public Object getValueAt(int row, int column) throws IllegalStateException 
	{
		if(!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");
		
		try{
			resultSet.absolute(row+1);
			return resultSet.getObject(column+1);
		}
		catch(SQLException ex){
			ex.printStackTrace();
		}
		
		return "";
	}
	
	/// <summary>
	/// Deze method set de query
	/// </summary>
	///	<parameter name="query">de query zelf</parameter>
	/// <author>Flemming Bosch</author>
	public void setQuery(String query) throws SQLException,IllegalStateException
	{
		if(!connectedToDatabase)
			throw new IllegalStateException("Not connected to Database");
		
		resultSet=statement.executeQuery(query);
		metaData = resultSet.getMetaData();
		resultSet.last();
		numberOfRows = resultSet.getRow();
		
		fireTableStructureChanged();
	}
	
	/// <summary>
	/// Deze method verbreekt de verbinding met de database.
	/// </summary>
	/// <author>Flemming Bosch</author>
	public void disconnectFromDatabase()
	{
		if(connectedToDatabase){
			try{
				resultSet.close();
				statement.close();
				connection.close();
				connectedToDatabase=false;
			}
			catch(SQLException ex){
				ex.printStackTrace();
			}
		}
	}
}